package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.util.HttpClientUtil;
import com.atguigu.gmall.common.util.IpUtil;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.model.vo.user.LoginUserResponseVo;
import com.atguigu.gmall.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/9/8 22:35
 */
@RestController
@RequestMapping("/api/user")
public class LoginController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/passport/login")
    public Result login(@RequestBody UserInfo userInfo, HttpServletRequest request) {
        String ipAddress = IpUtil.getIpAddress(request);
        userInfo.setIpAddress(ipAddress);
        LoginUserResponseVo userResponseVo =userInfoService.login(userInfo);
        if (userResponseVo == null){
            return Result.build(null, ResultCodeEnum.LOGIN_ERROR);
        }
        return Result.ok(userResponseVo);
    }
    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader(value = "token", required = false) String token) {
        if (token == null || token.isEmpty()){
            return Result.fail(ResultCodeEnum.TOKEN_ERROR);
        }
        userInfoService.logout(token);
        return Result.ok();
    }
}
